package com.bdqn.petmalldemo.controller;


import com.bdqn.petmalldemo.bo.PetAddBo;
import com.bdqn.petmalldemo.bo.ProductUpdateBo;
import com.bdqn.petmalldemo.bo.SearchProductBo;
import com.bdqn.petmalldemo.common.ResultCodeEnum;
import com.bdqn.petmalldemo.common.ReturnResult;
import com.bdqn.petmalldemo.entity.Product;
import com.bdqn.petmalldemo.service.ProductService;
import com.bdqn.petmalldemo.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@Api(tags = "商品控制器")
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Autowired
   private ProductService productService;

    @ApiModelProperty(value = "模糊查询商品信息")
    @PostMapping(value = "/search")
    public ReturnResult searchProduct(SearchProductBo searchProductBo){
        Map<String, Object> map = productService.searchProduct(searchProductBo);
        ReturnResult returnResult = ReturnResult.ok().data(map);
        log.info(returnResult.toString());
        return returnResult;
    }

    @ApiModelProperty(value = "新增宠物")
    @PostMapping(value = "/add")
    public ReturnResult add(PetAddBo petAddBo){
        //校验token是否有效
        ReturnResult returnResult= CommonUtils.checkUserLogin(petAddBo.getToken());
        //新增参数的校验
        if (StringUtils.isEmpty(petAddBo.getName())||StringUtils.isEmpty(petAddBo.getStock())) {
            return ReturnResult.error().code(ResultCodeEnum.ARGS_NULL.getCode())
                    .msg(ResultCodeEnum.ARGS_NULL.getMsg());
        }
        productService.addPet(petAddBo);
        return returnResult;
    }

    @ApiOperation(value = "图片文件上传")
    @PostMapping(value = "/upload")
    public ReturnResult upload(String token, MultipartFile file) throws Exception {
        //校验token是否有效
        ReturnResult returnResult= CommonUtils.checkUserLogin(token);
        //执行文件上传
        String saveFileName = file.getOriginalFilename();
        //获取文件后缀开始下标
        int beginIndex=saveFileName.lastIndexOf(".");
        //如果没有后缀名,就直接返回错误码
        if(beginIndex<=0){
            return ReturnResult.error().code(ResultCodeEnum.FILE_TYPE_NULL.getCode())
                    .msg(ResultCodeEnum.FILE_TYPE_NULL.getMsg());
        }
        //获取文件类型
        String fileType=saveFileName.substring(beginIndex);
        //生成新的文件名,以防重复
        String newFileName=UUID.randomUUID().toString().replaceAll("-","")+ fileType;
        //获取项目中指定的目录路径
        String path = ResourceUtils.getFile("classpath:static").getPath().replaceAll("%20", "");
        //创建上传文件目录
        File saveFile = new File( path+ "/upload/product/"+newFileName);
        //判断当前上传目录是否存在
        if (!saveFile.getParentFile().exists()) {
            //如果上传目录不存在,自动创建,并且一次创建多级目录
            saveFile.getParentFile().mkdirs();
        }
        //直接调用 MultipartFiel的上传接口
        file.transferTo(saveFile);
        //返回给前端的文件名
        return returnResult.data(newFileName);
    }
    @ApiOperation(value = "商品编辑前的查询")
    @PostMapping(value = "/get")
    public ReturnResult getProductById(@ApiParam(name = "id",value = "商品Id",required = true)
                                       String id){
        //参数的非空校验
        if (StringUtils.isEmpty(id)) {
            return ReturnResult.error().code(ResultCodeEnum.ARGS_NULL.getCode())
                                .msg(ResultCodeEnum.ARGS_NULL.getMsg());
        }
        //执行查询
        Product product = productService.getById(id);
        return ReturnResult.ok().data(product);
    }
    @ApiOperation(value = "商品编辑")
    @PostMapping(value = "/update")
    public ReturnResult update(ProductUpdateBo productUpdateBo){
        //校验token是否有效
        ReturnResult returnResult= CommonUtils.checkUserLogin(productUpdateBo.getToken());
        //新增参数的校验
        if (StringUtils.isEmpty(productUpdateBo.getName())||StringUtils.isEmpty(productUpdateBo.getStock())) {
            return ReturnResult.error().code(ResultCodeEnum.ARGS_NULL.getCode())
                    .msg(ResultCodeEnum.ARGS_NULL.getMsg());
        }
        productService.updateProduct(productUpdateBo);
        return  returnResult;
    }

    @ApiOperation(value = "商品删除")
    @PostMapping(value = "/delete")
    public ReturnResult delete(String token,String productId){
        //校验token是否有效
        ReturnResult returnResult= CommonUtils.checkUserLogin(token);
        productService.deleteProductById(productId);
        return returnResult;
    }

    @ApiOperation(value = "发布新增商品")
    @PostMapping(value = "/updateStatus")
    public ReturnResult updateStatus(String token,String id,Integer status){
        //校验token是否有效
        ReturnResult returnResult= CommonUtils.checkUserLogin(token);
        productService.updateProductStatus(id,status);
        return returnResult;
    }

    @ApiOperation(value = "运营人员审核")
    @PostMapping(value = "/audit")
    public ReturnResult audit(String token,Integer status,String productId){
        //校验token是否有效
        ReturnResult returnResult= CommonUtils.checkUserLogin(token);
        Product product = new Product();
        product.setId(productId)
                .setStatus(status);
        productService.updateById(product);
        return returnResult;
    }


    @ApiOperation(value = "设置精品")
    @PostMapping(value = "/recommend")
    public ReturnResult recommendProduct(String token,boolean recommend,String id){
        //校验当前用户的登录
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        Product product=new Product();
        product.setId(id)
                .setRecommend(recommend);
        productService.updateById(product);
        return returnResult;

    }

    @ApiOperation(value = "增加浏览记录数")
    @PostMapping(value = "/addViewNum")
    public ReturnResult addViewNum(String productId,Long categoryId){
        productService.addViewNum(productId,categoryId);
        //更新
        return ReturnResult.ok();
    }


}


